home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / NetPipedInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  2.9 KB  |  170 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InterruptedIOException;
  6.  
  7. class NetPipedInputStream extends InputStream {
  8.    boolean closed = true;
  9.    boolean connbroken = true;
  10.    Thread readSide;
  11.    Thread writeSide;
  12.    private byte[] buffer = new byte[1024];
  13.    // $FF: renamed from: in int
  14.    int field_0 = -1;
  15.    int out;
  16.  
  17.    public NetPipedInputStream(NetPipedOutputStream src) throws IOException {
  18.       this.connect(src);
  19.    }
  20.  
  21.    public NetPipedInputStream() {
  22.    }
  23.  
  24.    public void connect(NetPipedOutputStream src) throws IOException {
  25.       src.connect(this);
  26.    }
  27.  
  28.    synchronized void receive(int b) throws IOException {
  29.       int waitValue = 5;
  30.       this.writeSide = Thread.currentThread();
  31.  
  32.       while(this.field_0 == this.out) {
  33.          if (this.readSide != null && !this.readSide.isAlive()) {
  34.             throw new IOException("Pipe broken");
  35.          }
  36.  
  37.          this.notifyAll();
  38.          if (this.connbroken) {
  39.             throw new IOException("Lost connection to server");
  40.          }
  41.  
  42.          try {
  43.             if (waitValue < 640) {
  44.                waitValue *= 2;
  45.             }
  46.  
  47.             this.wait((long)waitValue);
  48.          } catch (InterruptedException var3) {
  49.             throw new InterruptedIOException();
  50.          }
  51.       }
  52.  
  53.       if (this.field_0 < 0) {
  54.          this.field_0 = 0;
  55.          this.out = 0;
  56.       }
  57.  
  58.       this.buffer[this.field_0++] = (byte)(b & 255);
  59.       if (this.field_0 >= this.buffer.length) {
  60.          this.field_0 = 0;
  61.       }
  62.  
  63.    }
  64.  
  65.    synchronized void receive(byte[] b, int off, int len) throws IOException {
  66.       while(true) {
  67.          --len;
  68.          if (len < 0) {
  69.             return;
  70.          }
  71.  
  72.          this.receive(b[off++]);
  73.       }
  74.    }
  75.  
  76.    synchronized void receivedLast() {
  77.       this.closed = true;
  78.       this.notifyAll();
  79.    }
  80.  
  81.    synchronized void connBroken() {
  82.       this.connbroken = true;
  83.       this.notify();
  84.    }
  85.  
  86.    public synchronized int read() throws IOException {
  87.       int trials = 2;
  88.       int waitValue = 5;
  89.  
  90.       while(this.field_0 < 0) {
  91.          this.readSide = Thread.currentThread();
  92.          if (this.writeSide != null && !this.writeSide.isAlive()) {
  93.             --trials;
  94.             if (trials < 0) {
  95.                throw new IOException("Pipe broken");
  96.             }
  97.          }
  98.  
  99.          if (this.closed) {
  100.             return -1;
  101.          }
  102.  
  103.          if (this.connbroken) {
  104.             throw new IOException("Lost connection to server");
  105.          }
  106.  
  107.          this.notifyAll();
  108.  
  109.          try {
  110.             if (waitValue < 640) {
  111.                waitValue *= 2;
  112.             }
  113.  
  114.             this.wait((long)waitValue);
  115.          } catch (InterruptedException var4) {
  116.             throw new InterruptedIOException();
  117.          }
  118.       }
  119.  
  120.       int ret = this.buffer[this.out++] & 255;
  121.       if (this.out >= this.buffer.length) {
  122.          this.out = 0;
  123.       }
  124.  
  125.       if (this.field_0 == this.out) {
  126.          this.field_0 = -1;
  127.       }
  128.  
  129.       return ret;
  130.    }
  131.  
  132.    public synchronized int read(byte[] b, int off, int len) throws IOException {
  133.       if (len <= 0) {
  134.          return 0;
  135.       } else {
  136.          int c = this.read();
  137.          if (c < 0) {
  138.             return -1;
  139.          } else {
  140.             b[off] = (byte)c;
  141.             int rlen = 1;
  142.  
  143.             while(this.field_0 >= 0) {
  144.                --len;
  145.                if (len <= 0) {
  146.                   break;
  147.                }
  148.  
  149.                b[off + rlen] = this.buffer[this.out++];
  150.                ++rlen;
  151.                if (this.out >= this.buffer.length) {
  152.                   this.out = 0;
  153.                }
  154.  
  155.                if (this.field_0 == this.out) {
  156.                   this.field_0 = -1;
  157.                }
  158.             }
  159.  
  160.             return rlen;
  161.          }
  162.       }
  163.    }
  164.  
  165.    public void close() throws IOException {
  166.       this.field_0 = -1;
  167.       this.closed = true;
  168.    }
  169. }
  170.